#!/bin/bash -x

kata_coco_top_dir="$HOME/workspace/CoCo"

trustee_operator_repo_url="https://gitee.com/hanliyang-kata-coco/trustee-operator.git"
trustee_operator_repo_branch="0.3.0-hygon-arch-sig"
trustee_operator_repo_dir="${kata_coco_top_dir}/trustee-operator"

trustee_operator_controller_dir="${trustee_operator_repo_dir}/config/default"
trustee_operator_controller_manager_yaml="${trustee_operator_repo_dir}/config/manager/manager.yaml"
trustee_operator_microservices_dir="${trustee_operator_repo_dir}/config/samples/microservices"
trustee_operator_k8s_namespace="trustee-operator-system"
trustee_operator_controller_k8s_podname_prefix="trustee-operator-controller-manager"
trustee_operator_microservices_deployment_k8s_podname_prefix="trustee-deployment"
trustee_operator_kbs_uri=""
trustee_operator_as_uri=""
trustee_operator_rvps_uri=""

usage () {
  set +x
  echo -e "\033[31m
Usage: $0 [-h] [-k <kbs-registry>] [-a <as-registry>] [-r <rvps-registry>]

       -h,--help Display this help message
       -k,--kbs  The kbs (Key-Broker-Service) payload image.
                 Default is docker.io/robertsonhan/kbs-grpc-as:trusteev0.11.0
       -a,--as   The as (Attestation-Service) payload image.
                 Default is docker.io/robertsonhan/coco-as-grpc:trusteev0.11.0
       -r,--rvps The rvps (Reference-Value-Provider-Service) payload image.
                 Default is docker.io/robertsonhan/rvps:trusteev0.11.0
\033[0m"
  set -x
}

params=$(getopt -o hk:a:r: --long help,kbs:as:rvps: -- "$@")
if [ $? -ne 0 ]; then
  echo "[Error] cannot parse parameters"
  exit 1
fi

eval set -- "$params"

while true; do
  case "$1" in
  -h|--help)
    usage
    exit 0
    ;;
  -k|--kbs)
    trustee_operator_kbs_uri="$2"
    shift 2
    ;;
  -a|--as)
    trustee_operator_as_uri="$2"
    shift 2
    ;;
  -r|--rvps)
    trustee_operator_rvps_uri="$2"
    shift 2
    ;;
  --)
    shift
    break
    ;;
  *)
    usage
    exit 1
    ;;
  esac
done

if [ ! -e ${trustee_operator_repo_dir} ]; then
  command git help > /dev/null
  if [ $? -ne 0 ]; then
    sudo yum install -y git
  fi

  mkdir -p $(dirname ${trustee_operator_repo_dir}) || true

  git clone ${trustee_operator_repo_url} \
    -b ${trustee_operator_repo_branch} \
    ${trustee_operator_repo_dir}
fi

revoke_trustee_operator_controller () {
  echo "[Revoke trustee-operator controller]"
  kubectl delete -k ${trustee_operator_controller_dir}
}

revoke_trustee_operator_microservices () {
  echo "[Revoke trustee-operator microservices]"
  kubectl delete -k ${trustee_operator_microservices_dir}
}

run_trustee_operator_controller () {
  echo "[Revoke staled trustee-operator]"
  revoke_trustee_operator_microservices
  revoke_trustee_operator_controller

  echo "[Update the kbs & as & rvps payload image uri]"
  if [ "X${trustee_operator_kbs_uri}" != "X" ]; then
    sed -i "s!\(value: \)docker.io/robertsonhan/kbs-grpc-as:trusteev0.11.0!${trustee_operator_kbs_uri}!g" \
      ${trustee_operator_controller_manager_yaml}
  fi
  if [ "X${trustee_operator_as_uri}" != "X" ]; then
    sed -i "s!\(value: \)docker.io/robertsonhan/coco-as-grpc:trusteev0.11.0!${trustee_operator_as_uri}!g" \
      ${trustee_operator_controller_manager_yaml}
  fi
  if [ "X${trustee_operator_rvps_uri}" != "X" ]; then
    sed -i "s!\(value: \)docker.io/robertsonhan/rvps:trusteev0.11.0!${trustee_operator_rvps_uri}!g" \
      ${trustee_operator_controller_manager_yaml}
  fi

  echo "[Run trustee-operator controller]"
  kubectl apply -k ${trustee_operator_controller_dir}

  wait_max_secs=300
  wait_cnt=0
  while [ $(kubectl get pods -n ${trustee_operator_k8s_namespace} | \
    grep ${trustee_operator_controller_k8s_podname_prefix} | \
    grep -c "\<Running\>") -eq 0 ]; do
    sleep 1
    let wait_cnt++
    if [ ${wait_cnt} -gt ${wait_max_secs} ]; then
      revoke_trustee_operator_controller
      echo "Run trustee-operator controller [FAIL], you need debug the root cause"
      exit 1
    fi
  done

  # show trustee-operator controller status
  sleep 10
  kubectl get pods -n ${trustee_operator_k8s_namespace}
}

run_trustee_operator_controller

run_trustee_operator_microservices () {
  echo "[Run trustee-operator microservices]"

  pushd ${trustee_operator_microservices_dir}

  # create authentication keys
  openssl genpkey -algorithm ed25519 > privateKey
  openssl pkey -in privateKey -pubout -out kbs.pem

  kubectl apply -k .

  wait_max_secs=600
  wait_cnt=0
  while [ $(kubectl get pods -n ${trustee_operator_k8s_namespace} | \
    grep ${trustee_operator_microservices_deployment_k8s_podname_prefix} | \
    grep -c "\<Running\>") -eq 0 ]; do
    sleep 1
    let wait_cnt++
    if [ ${wait_cnt} -gt ${wait_max_secs} ]; then
      revoke_trustee_operator_microservices
      revoke_trustee_operator_controller
      echo "Run trustee-operator microservices [FAIL], you need debug the root cause"
      exit 1
    fi
  done

  # show trustee-operator microservices status
  sleep 10
  kubectl get pods -n ${trustee_operator_k8s_namespace}

  popd # ${trustee_operator_microservices_dir}

  # Use hygon tools to initialize `as rego policy` and `rvps ref value json`.
  # I the following tests, we will update the `as rego policy` and
  # `rvps ref value json` if necessary.
  pushd ${trustee_operator_microservices_dir}/hygon
  ./tool-operator-on-kbs-as-rvps.sh as "tee"
  ./tool-operator-on-kbs-as-rvps.sh rvps "tee"
  popd # ${trustee_operator_microservices_dir}/hygon
}

run_trustee_operator_microservices
